﻿Imports System.Data.SqlClient
Public Class Mixture
    Dim sqlcon As New SqlConnection
    Dim dataRead As SqlDataReader
    Dim com As New SqlCommand
    Dim i As Integer
    Dim UpdateButtonFlag As Boolean = 0

    Private Sub cb_add_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub btu_new_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btu_new.Click
        LoginForm1.Show()
        ''add_mix.Show()

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       'RaiseAlarm(True, True, True, "Unit", "Desc", 1, "Unit Desc Code", Label1)
        Dim dt As DataTable
        dt = GetTable("SELECT * FROM vwBatchs")
        dgv_mix.DataSource = dt
        cb_mix_no.DataSource = dt
        cb_mix_no.DisplayMember = "BatchName"
        cb_mix_no.ValueMember = "BatchID"
        dgv_mix.Columns(1).Visible = False
        dgv_mix.Columns(2).HeaderText = "اسم الخلطة"
        dgv_mix.Columns(3).HeaderText = "كود الخلطة"
        dgv_mix.Columns(1).Width = dgv_mix.Width / 2
        dgv_mix.Columns(2).Width = dgv_mix.Width / 2
        btnDeleteMix.Enabled = False
    End Sub

    Private Sub btu_select_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    End Sub

    Private Sub btu_update_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btu_update.Click

        LoginForm2.Show()

        ''If UpdateButtonFlag = 0 Then
        ''    UpdateButtonFlag = 1
        ''    gb_1.Enabled = True
        ''    gb_2.Enabled = True
        ''    btnSaveUpdate.Enabled = True
        ''Else
        ''    UpdateButtonFlag = 0
        ''    gb_1.Enabled = False
        ''    gb_2.Enabled = False
        ''    btnSaveUpdate.Enabled = False
        ''End If




    End Sub

    Private Sub التقاريرToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles التقاريرToolStripMenuItem.Click
        reports2.Show()
    End Sub

    Private Sub الاToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles الاToolStripMenuItem.Click
        alarms.Show()
    End Sub

    Private Sub الرئيسيةToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles الرئيسيةToolStripMenuItem.Click
        main.Show()
        Me.Close()
    End Sub

    Private Sub btn_ok_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_ok.Click
        If cb_mix_no.Text = "" Then
            MessageBox.Show("لم يتم اختيار الخلطة", "تنبيه", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Else

            Dim dt As DataTable
            dt = GetTable("SELECT * FROM BatchsDetails WHERE BatchID='" & GetTable("SELECT * FROM Batchs WHERE BatchName='" & cb_mix_no.Text & "'").Rows(0).Item(0) & "'")

            txt_total_corn1.Text = dt.Rows(0).Item("MixWeight")
            txt_h_corn1.Text = dt.Rows(0).Item("HighWeight")
            txt_l_corn1.Text = Val((dt.Rows(0).Item("LowWeight")) + (dt.Rows(0).Item("FreeFallWeight")))
            txt_f_corn1.Text = dt.Rows(0).Item("FreeFallWeight")
            corn1_sp_h.Text = Val((dt.Rows(0).Item("HighSpeed")) / 5)
            corn1_sp_l.Text = Val((dt.Rows(0).Item("LowSpeed")) / 5)

            txt_total_corn2.Text = dt.Rows(1).Item("MixWeight")
            txt_h_corn2.Text = dt.Rows(1).Item("HighWeight")
            txt_l_corn2.Text = Val((dt.Rows(1).Item("LowWeight")) + (dt.Rows(1).Item("FreeFallWeight")))
            txt_f_corn2.Text = dt.Rows(1).Item("FreeFallWeight")
            corn2_sp_h.Text = Val((dt.Rows(1).Item("HighSpeed")) / 5)
            corn2_sp_l.Text = Val((dt.Rows(1).Item("LowSpeed")) / 5)

            txt_total_soy1.Text = dt.Rows(2).Item("MixWeight")
            txt_h_soy1.Text = dt.Rows(2).Item("HighWeight")
            txt_l_soy1.Text = Val((dt.Rows(2).Item("LowWeight")) + (dt.Rows(2).Item("FreeFallWeight")))
            txt_f_soy1.Text = dt.Rows(2).Item("FreeFallWeight")
            soy1_sp_h.Text = Val((dt.Rows(2).Item("HighSpeed")) / 5)
            soy1_sp_l.Text = Val((dt.Rows(2).Item("LowSpeed")) / 5)

            txt_total_soy2.Text = dt.Rows(3).Item("MixWeight")
            txt_h_soy2.Text = dt.Rows(3).Item("HighWeight")
            txt_l_soy2.Text = Val((dt.Rows(3).Item("LowWeight")) + (dt.Rows(3).Item("FreeFallWeight")))
            txt_f_soy2.Text = dt.Rows(3).Item("FreeFallWeight")
            soy2_sp_h.Text = Val((dt.Rows(3).Item("HighSpeed")) / 5)
            soy2_sp_l.Text = Val((dt.Rows(3).Item("LowSpeed")) / 5)

            txt_total_sn.Text = dt.Rows(4).Item("MixWeight")
            txt_h_sn.Text = dt.Rows(4).Item("HighWeight")
            txt_l_sn.Text = Val((dt.Rows(4).Item("LowWeight")) + (dt.Rows(4).Item("FreeFallWeight")))
            txt_f_sn.Text = dt.Rows(4).Item("FreeFallWeight")
            sn_sp_h.Text = Val((dt.Rows(4).Item("HighSpeed")) / 5)
            sn_sp_l.Text = Val((dt.Rows(4).Item("LowSpeed")) / 5)

            txt_total_rada.Text = dt.Rows(5).Item("MixWeight")
            txt_h_rada.Text = dt.Rows(5).Item("HighWeight")
            txt_l_rada.Text = Val((dt.Rows(5).Item("LowWeight")) + (dt.Rows(5).Item("FreeFallWeight")))
            txt_f_rada.Text = dt.Rows(5).Item("FreeFallWeight")
            rada_sp_h.Text = Val((dt.Rows(5).Item("HighSpeed")) / 5)
            rada_sp_l.Text = Val((dt.Rows(5).Item("LowSpeed")) / 5)

            txt_total_con.Text = dt.Rows(6).Item("MixWeight")
            txt_h_con.Text = dt.Rows(6).Item("HighWeight")
            txt_l_con.Text = Val((dt.Rows(6).Item("LowWeight")) + (dt.Rows(6).Item("FreeFallWeight")))
            txt_f_con.Text = dt.Rows(6).Item("FreeFallWeight")
            con_sp_h.Text = Val((dt.Rows(6).Item("HighSpeed")) / 5)
            con_sp_l.Text = Val((dt.Rows(6).Item("LowSpeed")) / 5)

            txt_total_gelotine.Text = dt.Rows(7).Item("MixWeight")
            txt_h_gelotine.Text = dt.Rows(7).Item("HighWeight")
            txt_l_gelotine.Text = Val((dt.Rows(7).Item("LowWeight")) + (dt.Rows(7).Item("FreeFallWeight")))
            txt_f_gelotine.Text = dt.Rows(7).Item("FreeFallWeight")
            gelotine_sp_h.Text = Val((dt.Rows(7).Item("HighSpeed")) / 5)
            gelotine_sp_l.Text = Val((dt.Rows(7).Item("LowSpeed")) / 5)

            txt_total_oil.Text = dt.Rows(8).Item("MixWeight")
            txt_h_oil.Text = dt.Rows(8).Item("HighWeight")
            txt_l_oil.Text = Val((dt.Rows(8).Item("LowWeight")) + (dt.Rows(8).Item("FreeFallWeight")))
            txt_f_oil.Text = dt.Rows(8).Item("FreeFallWeight")
            oil_sp_h.Text = Val((dt.Rows(8).Item("HighSpeed")) / 5)
            oil_sp_l.Text = Val((dt.Rows(8).Item("LowSpeed")) / 5)

            txt_total_add.Text = dt.Rows(9).Item("MixWeight")
            txt_h_add.Text = dt.Rows(9).Item("HighWeight")
            txt_l_add.Text = Val((dt.Rows(9).Item("LowWeight")) + (dt.Rows(9).Item("FreeFallWeight")))
            txt_f_add.Text = dt.Rows(9).Item("FreeFallWeight")
            add_sp_h.Text = Val((dt.Rows(9).Item("HighSpeed")) / 5)
            add_sp_l.Text = Val((dt.Rows(9).Item("LowSpeed")) / 5)

            txt_total_ex1.Text = dt.Rows(10).Item("MixWeight")
            txt_h_ex1.Text = dt.Rows(10).Item("HighWeight")
            txt_l_ex1.Text = Val((dt.Rows(10).Item("LowWeight")) + (dt.Rows(10).Item("FreeFallWeight")))
            txt_f_ex1.Text = dt.Rows(10).Item("FreeFallWeight")
            ex1_sp_h.Text = Val((dt.Rows(10).Item("HighSpeed")) / 5)
            ex1_sp_l.Text = Val((dt.Rows(10).Item("LowSpeed")) / 5)

            txt_total_ex2.Text = dt.Rows(11).Item("MixWeight")
            txt_h_ex2.Text = dt.Rows(11).Item("HighWeight")
            txt_l_ex2.Text = Val((dt.Rows(11).Item("LowWeight")) + (dt.Rows(11).Item("FreeFallWeight")))
            txt_f_ex2.Text = dt.Rows(11).Item("FreeFallWeight")
            ex2_sp_h.Text = Val((dt.Rows(11).Item("HighSpeed")) / 5)
            ex2_sp_l.Text = Val((dt.Rows(11).Item("LowSpeed")) / 5)


            main.txtTank1s.Text = dt.Rows(0).Item("MixWeight")
            main.txtTank2s.Text = dt.Rows(1).Item("MixWeight")
            main.txtTank3s.Text = dt.Rows(2).Item("MixWeight")
            main.txtTank4s.Text = dt.Rows(3).Item("MixWeight")
            main.txtTank5s.Text = dt.Rows(4).Item("MixWeight")
            main.txtTank6s.Text = dt.Rows(5).Item("MixWeight")
            main.txtTank7s.Text = dt.Rows(6).Item("MixWeight")
            main.txtTank8s.Text = dt.Rows(7).Item("MixWeight")
            main.txtTank9s.Text = dt.Rows(8).Item("MixWeight")
            main.txtTank10s.Text = dt.Rows(9).Item("MixWeight")
            main.txtTank11s.Text = dt.Rows(10).Item("MixWeight")
            main.txtTank12s.Text = dt.Rows(11).Item("MixWeight")

            For i As Integer = 0 To 11
                main.TanksSpeedsLow(i) = dt.Rows(i)("LowSpeed")
                main.TanksSpeedsHigh(i) = dt.Rows(i)("HighSpeed")
                main.TanksSpeedsTotalDrop(i) = dt.Rows(i)("MixWeight") - dt.Rows(i)("FreeFallWeight")
                main.TanksSpeedsHighDrop(i) = dt.Rows(i)("HighWeight")
            Next


            main.txtScaled.Text = Val(main.txtTank1s.Text) + Val(main.txtTank2s.Text) + Val(main.txtTank3s.Text) + Val(main.txtTank4s.Text) +
                Val(main.txtTank5s.Text) + Val(main.txtTank6s.Text) + Val(main.txtTank7s.Text) + Val(main.txtTank8s.Text) + Val(main.txtTank9s.Text) +
                Val(main.txtTank10s.Text) + Val(main.txtTank11s.Text) + Val(main.txtTank12s.Text)

            main.lblBatchName.Text = cb_mix_no.Text
            '"Batch Name: "
            main.BatchNo = cb_mix_no.SelectedValue

            If Txt_no_mix.Text = "" Then
                main.txtNumberOfBatchesLeft.Text = "1"
            Else
                main.txtNumberOfBatchesLeft.Text = Txt_no_mix.Text
            End If
        End If
       

    End Sub


    Private Sub btnSaveUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveUpdate.Click

        Dim BatchID As Integer
        BatchID = GetTable("SELECT * FROM Batchs WHERE BatchName='" & cb_mix_no.Text & "'").Rows(0).Item(0)

        ExecuteSQL("UPDATE [BatchsDetails] SET [MixWeight] ='" & txt_total_corn1.Text & "' ,[LowSpeed] ='" & Val((corn1_sp_l.Text) * 5) & "' ,[HighSpeed] ='" & Val((corn1_sp_h.Text) * 5) & "' ,[LowWeight] ='" & Val((txt_l_corn1.Text) - (txt_f_corn1.Text)) & "' ,[HighWeight] ='" & txt_h_corn1.Text & "' ,[FreeFallWeight] ='" & txt_f_corn1.Text & "' WHERE BatchID='" & BatchID & "' AND TankID='" & "1" & "'")
        ExecuteSQL("UPDATE [BatchsDetails] SET [MixWeight] ='" & txt_total_corn2.Text & "' ,[LowSpeed] ='" & Val((corn2_sp_l.Text) * 5) & "' ,[HighSpeed] ='" & Val((corn2_sp_h.Text) * 5) & "' ,[LowWeight] ='" & Val((txt_l_corn2.Text) - (txt_f_corn2.Text)) & "' ,[HighWeight] ='" & txt_h_corn2.Text & "' ,[FreeFallWeight] ='" & txt_f_corn2.Text & "' WHERE BatchID='" & BatchID & "' AND TankID='" & "2" & "'")
        ExecuteSQL("UPDATE [BatchsDetails] SET [MixWeight] ='" & txt_total_soy1.Text & "' ,[LowSpeed] ='" & Val((soy1_sp_l.Text) * 5) & "' ,[HighSpeed] ='" & Val((soy1_sp_h.Text) * 5) & "' ,[LowWeight] ='" & Val((txt_l_soy1.Text) - (txt_f_soy1.Text)) & "' ,[HighWeight] ='" & txt_h_soy1.Text & "' ,[FreeFallWeight] ='" & txt_f_soy1.Text & "' WHERE BatchID='" & BatchID & "' AND TankID='" & "3" & "'")
        ExecuteSQL("UPDATE [BatchsDetails] SET [MixWeight] ='" & txt_total_soy2.Text & "' ,[LowSpeed] ='" & Val((soy2_sp_l.Text) * 5) & "' ,[HighSpeed] ='" & Val((soy2_sp_h.Text) * 5) & "' ,[LowWeight] ='" & Val((txt_l_soy2.Text) - (txt_f_soy2.Text)) & "' ,[HighWeight] ='" & txt_h_soy2.Text & "' ,[FreeFallWeight] ='" & txt_f_soy2.Text & "' WHERE BatchID='" & BatchID & "' AND TankID='" & "4" & "'")
        ExecuteSQL("UPDATE [BatchsDetails] SET [MixWeight] ='" & txt_total_sn.Text & "' ,[LowSpeed] ='" & Val((sn_sp_l.Text) * 5) & "' ,[HighSpeed] ='" & Val((sn_sp_h.Text) * 5) & "' ,[LowWeight] ='" & Val((txt_l_sn.Text) - (txt_f_sn.Text)) & "' ,[HighWeight] ='" & txt_h_sn.Text & "' ,[FreeFallWeight] ='" & txt_f_sn.Text & "' WHERE BatchID='" & BatchID & "' AND TankID='" & "5" & "'")
        ExecuteSQL("UPDATE [BatchsDetails] SET [MixWeight] ='" & txt_total_rada.Text & "' ,[LowSpeed] ='" & Val((rada_sp_l.Text) * 5) & "' ,[HighSpeed] ='" & Val((rada_sp_h.Text) * 5) & "' ,[LowWeight] ='" & Val((txt_l_rada.Text) - (txt_f_rada.Text)) & "' ,[HighWeight] ='" & txt_h_rada.Text & "' ,[FreeFallWeight] ='" & txt_f_rada.Text & "' WHERE BatchID='" & BatchID & "' AND TankID='" & "6" & "'")
        ExecuteSQL("UPDATE [BatchsDetails] SET [MixWeight] ='" & txt_total_con.Text & "' ,[LowSpeed] ='" & Val((con_sp_l.Text) * 5) & "' ,[HighSpeed] ='" & Val((con_sp_h.Text) * 5) & "' ,[LowWeight] ='" & Val((txt_l_con.Text) - (txt_f_con.Text)) & "' ,[HighWeight] ='" & txt_h_con.Text & "' ,[FreeFallWeight] ='" & txt_f_con.Text & "' WHERE BatchID='" & BatchID & "' AND TankID='" & "7" & "'")
        ExecuteSQL("UPDATE [BatchsDetails] SET [MixWeight] ='" & txt_total_gelotine.Text & "' ,[LowSpeed] ='" & Val((gelotine_sp_l.Text) * 5) & "' ,[HighSpeed] ='" & Val((gelotine_sp_h.Text) * 5) & "' ,[LowWeight] ='" & Val((txt_l_gelotine.Text) - (txt_f_gelotine.Text)) & "' ,[HighWeight] ='" & txt_h_gelotine.Text & "' ,[FreeFallWeight] ='" & txt_f_gelotine.Text & "' WHERE BatchID='" & BatchID & "' AND TankID='" & "8" & "'")
        ExecuteSQL("UPDATE [BatchsDetails] SET [MixWeight] ='" & txt_total_oil.Text & "' ,[LowSpeed] ='" & Val((oil_sp_l.Text) * 5) & "' ,[HighSpeed] ='" & Val((oil_sp_h.Text) * 5) & "' ,[LowWeight] ='" & Val((txt_l_oil.Text) - (txt_f_oil.Text)) & "' ,[HighWeight] ='" & txt_h_oil.Text & "' ,[FreeFallWeight] ='" & txt_f_oil.Text & "' WHERE BatchID='" & BatchID & "' AND TankID='" & "9" & "'")
        ExecuteSQL("UPDATE [BatchsDetails] SET [MixWeight] ='" & txt_total_add.Text & "' ,[LowSpeed] ='" & Val((add_sp_l.Text) * 5) & "' ,[HighSpeed] ='" & Val((add_sp_h.Text) * 5) & "' ,[LowWeight] ='" & Val((txt_l_add.Text) - (txt_f_add.Text)) & "' ,[HighWeight] ='" & txt_h_add.Text & "' ,[FreeFallWeight] ='" & txt_f_add.Text & "' WHERE BatchID='" & BatchID & "' AND TankID='" & "10" & "'")
        ExecuteSQL("UPDATE [BatchsDetails] SET [MixWeight] ='" & txt_total_ex1.Text & "' ,[LowSpeed] ='" & Val((ex1_sp_l.Text) * 5) & "' ,[HighSpeed] ='" & Val((ex1_sp_h.Text) * 5) & "' ,[LowWeight] ='" & Val((txt_l_ex1.Text) - (txt_f_ex1.Text)) & "' ,[HighWeight] ='" & txt_h_ex1.Text & "' ,[FreeFallWeight] ='" & txt_f_ex1.Text & "' WHERE BatchID='" & BatchID & "' AND TankID='" & "11" & "'")
        ExecuteSQL("UPDATE [BatchsDetails] SET [MixWeight] ='" & txt_total_ex2.Text & "' ,[LowSpeed] ='" & Val((ex2_sp_l.Text) * 5) & "' ,[HighSpeed] ='" & Val((ex2_sp_h.Text) * 5) & "' ,[LowWeight] ='" & Val((txt_l_ex2.Text) - (txt_f_ex2.Text)) & "' ,[HighWeight] ='" & txt_h_ex2.Text & "' ,[FreeFallWeight] ='" & txt_f_ex2.Text & "' WHERE BatchID='" & BatchID & "' AND TankID='" & "12" & "'")
        MessageBox.Show("تم تعديل البيانات")
        gb_1.Enabled = False
        gb_2.Enabled = False
        btnSaveUpdate.Enabled = False

    End Sub

    ' ''Private Sub btn_test_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_test.Click
    ' ''    InsertBatchFinal(cb_mix_no.Text, main.txtTank1d.Text, main.txtTank2d.Text, main.txtTank3d.Text, main.txtTank4d.Text, main.txtTank5d.Text, main.txtTank6d.Text, main.txtTank7d.Text, main.txtTank8d.Text, main.txtTank9d.Text, main.txtTank10d.Text, main.txtTank11d.Text, main.txtTank12d.Text, main.txtScales.Text, main.DTB1.Value)
    ' ''End Sub

    Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefresh.Click
        Dim dt As DataTable
        dt = GetTable("SELECT * FROM vwBatchs")
        dgv_mix.DataSource = dt
        cb_mix_no.DataSource = dt
        cb_mix_no.DisplayMember = "BatchName"
        cb_mix_no.ValueMember = "BatchID"
        dgv_mix.Columns(1).Visible = False
        dgv_mix.Columns(2).HeaderText = "اسم الخلطة"
        dgv_mix.Columns(3).HeaderText = "كود الخلطة"
        dgv_mix.Columns(1).Width = dgv_mix.Width / 2
        dgv_mix.Columns(2).Width = dgv_mix.Width / 2
    End Sub


    Private Sub btnDeleteMix_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeleteMix.Click
        If MsgBox("Are you sure to delete?", MsgBoxStyle.YesNo, "Confirm Deletion") = MsgBoxResult.Yes Then
            Dim n As Integer
            n = dgv_mix.Rows.Count - 1

            For i As Integer = 0 To n
                If dgv_mix.Rows(i).Cells(0).Value = True Then
                    ExecuteSQL("DELETE FROM Alarms WHERE BatchID='" & dgv_mix.Rows(i).Cells(1).Value & "'")
                    ExecuteSQL("DELETE FROM BatchsDetails WHERE BatchID='" & dgv_mix.Rows(i).Cells(1).Value & "'")
                    Dim i2 As Integer
                    Dim dt2 As DataTable
                    dt2 = GetTable("SELECT * From BatchRecords Where BatchID='" & dgv_mix.Rows(i).Cells(1).Value & "'")
                    i2 = dt2.Rows.Count - 1
                    For i3 As Integer = 0 To i2
                        ExecuteSQL("DELETE FROM BatchRecordsDetails WHERE RecordID='" & dt2.Rows(i3).Item("RecordID") & "'")
                    Next
                    ExecuteSQL("DELETE FROM BatchRecords WHERE BatchID='" & dgv_mix.Rows(i).Cells(1).Value & "'")
                    ExecuteSQL("DELETE FROM Batchs WHERE BatchID='" & dgv_mix.Rows(i).Cells(1).Value & "'")
                End If
            Next
            dgv_mix.DataSource = GetTable("SELECT * From vwBatchs")
        End If
       
    End Sub

    Private Sub BtnActivateDeletion_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnActivateDeletion.Click
        If dgv_mix.ReadOnly = False Then
            dgv_mix.ReadOnly = True
            btnDeleteMix.Enabled = False
        Else
            dgv_mix.ReadOnly = False
            btnDeleteMix.Enabled = True
        End If
    End Sub
End Class